import pymysql
from pyecharts.charts import Pie
from pyecharts import options
# 连接数据库
db = pymysql.connect(
    host="localhost",
    user="root",
    password="",
    database="crawlerdemo",
    charset="utf8"
)
cursor = db.cursor()
#用来查找岗位数量最多的五个城市
sql="select job_place,count(job_id) from job group by job_place order by count(job_id) desc limit 5"
try:
    cursor.execute(sql)
    res = cursor.fetchall()
    # 计算总岗位数量
    total_jobs = sum([row[1] for row in res])
    # 计算每个城市的岗位数量占比
    data = [(row[0], round(row[1] / total_jobs * 100, 2)) for row in res]

    pie = Pie()
    # 添加数据到饼图
    pie.add("岗位数量占比", data)
    pie.set_global_opts(title_opts=options.TitleOpts(title="五个城市的岗位量占比"),
        tooltip_opts=options.TooltipOpts(formatter="{b}: {c}%"))
    pie.render("piera.html")
except:
    print("查询出错啦")